﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using AnalisisDeRiesgos.Dal;

namespace AnalisisDeRiesgos.Models.Process
{
    public class DetailsProcessModel
    {
        public int ProcessId { get; set; }
        public string Name { get; set; }
        public string Management { get; set; }       

        private riskanalysisEntities entities = new riskanalysisEntities();

        public DetailsProcessModel(int processId)
        {
            var query = from p in entities.processes
                        where p.processId == processId && p.enabled
                        select new
                        {
                            ProcessId = p.processId,
                            Name = p.name,                            
                            Management = p.management.name
                        };

            var theManagement = query.FirstOrDefault();

            if (theManagement == null)
                throw new DetailsProcessException("El proceso especificada no existe");

            ProcessId = theManagement.ProcessId;
            Name = theManagement.Name;            
            Management = theManagement.Management;
        }

        public void DeleteManagement()
        {
            var managementList = from p in entities.processes
                                 where p.processId == ProcessId && p.enabled
                                 select p;

            if (managementList.Count() <= 0)
                throw new DetailsProcessException("El proceso especificada no fue encontrada");

            managementList.First().enabled = false;
            entities.SaveChanges();
        }
    }
}